package com.wishwall.jackson;

import java.text.SimpleDateFormat;

import org.codehaus.jackson.map.AnnotationIntrospector;
import org.codehaus.jackson.map.DeserializationConfig.Feature;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.codehaus.jackson.map.introspect.JacksonAnnotationIntrospector;

public class JacksonContext {
	private static ObjectMapper objectMapper = null;

	private JacksonContext() {   }
	/**
	 * 
	 * @return
	 */
	public static synchronized ObjectMapper getObjectMapperInstance() {
            if (objectMapper == null) {
            	objectMapper = new ObjectMapper();
        		objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
        		objectMapper.configure(Feature.USE_ANNOTATIONS, true);
        		objectMapper.configure(SerializationConfig.Feature.SORT_PROPERTIES_ALPHABETICALLY, true);
        		objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        		//		AnnotationIntrospector primary = new JaxbAnnotationIntrospector();
        		AnnotationIntrospector secondary = new JacksonAnnotationIntrospector();
        		//		AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary);
        		objectMapper.setAnnotationIntrospector(secondary);
        		objectMapper.setSerializationInclusion(Inclusion.ALWAYS);
        		objectMapper.configure(org.codehaus.jackson.map.SerializationConfig.Feature.AUTO_DETECT_FIELDS,  false);
        		objectMapper.configure(org.codehaus.jackson.map.SerializationConfig.Feature.AUTO_DETECT_GETTERS,  false);
        		objectMapper.configure(org.codehaus.jackson.map.SerializationConfig.Feature.AUTO_DETECT_IS_GETTERS,  false);
        		objectMapper.configure(org.codehaus.jackson.map.SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS,  false);
            }
            return objectMapper;
    }

}
